package com.itheima.hmshopping.dubbo.apiImpl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.SmsHomeAdvertiseApi;
import com.itheima.hmshopping.dubbo.mapper.SmsHomeAdvertiseMapper;
import com.itheima.hmshopping.entity.SmsHomeAdvertise;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
@DubboService
public class SmsHomeAdvertiseImpl implements SmsHomeAdvertiseApi {
    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    /**
     * 添加广告
     *
     * @param smsHomeAdvertise
     */
    @Override
    public void create(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.insert(smsHomeAdvertise);
    }

    /**
     * 删除广告
     *
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        List<Long> list = new ArrayList<>();
        //遍历集合,将Integer转为Long
        for (Integer id : ids) {
            Long aLong = Convert.toLong(id);
            list.add(aLong);
        }
        smsHomeAdvertiseMapper.deleteBatchIds(list);
    }

    /**
     * 分页查询广告
     * @param name
     * @param type
     * @param pageNum
     * @param pageSize
     * @param endTime
     * @return
     */
    @Override
    public Page list(String name, Integer type, Integer pageNum, Integer pageSize, String endTime) {
        LocalDateTime localStartTime = null;
        LocalDateTime localEndTime = null;
        //非空判断
        if (endTime != null) {
            LocalDate beginDateTime = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            //开始时间
            localStartTime = beginDateTime.atStartOfDay();
            //结束时间(加一天)
            localEndTime = localStartTime.plusDays(1L);
        }
        Page<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        //构造条件
        LambdaQueryWrapper<SmsHomeAdvertise> wrapper = new LambdaQueryWrapper<>();
        //模糊查询
        wrapper.eq(type != null, SmsHomeAdvertise::getType, type);
        wrapper.between(endTime != null, SmsHomeAdvertise::getEndTime, localStartTime, localEndTime);
        wrapper.like(name != null, SmsHomeAdvertise::getName, name);
        smsHomeAdvertiseMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 修改上下线状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        smsHomeAdvertiseMapper.updateStatus(Convert.toLong(id),status);
    }

    /**
     * 修改广告
     * @param id
     * @param smsHomeAdvertise
     */
    @Override
    public void update(Integer id, SmsHomeAdvertise smsHomeAdvertise) {
       smsHomeAdvertiseMapper.updateById(smsHomeAdvertise) ;
    }

    /**
     * 获取广告详情
     * @param id
     */
    @Override
    public SmsHomeAdvertise getOne(Integer id) {
       return smsHomeAdvertiseMapper.selectById(Convert.toLong(id));
    }
}
